/**   
 * Copyright © 2017 金风科技. All rights reserved.
 * 
 * @Title: GlobalExceptionHandler.java 
 * @Prject: tgcw-c-common
 * @Package: com.goldwind.common.exception 
 * @Description: Controller异常捕获
 * @author: liangwenlu   
 * @date: 2019年3月25日 下午3:05:30
 * @version: V1.0   
 */
package com.gd.iot.energy.exception;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import cn.hutool.core.util.StrUtil;
import com.gd.iot.energy.code.ResponseCode;
import com.gd.iot.energy.util.MessageUtil;
import com.gd.iot.energy.util.ResponseCodeUtil;
import com.gd.iot.energy.util.ResponseUtil;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.shiro.authz.AuthorizationException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @ClassName: GlobalExceptionHandler
 * @Description: Controller异常捕获
 */
@ControllerAdvice
public class GlobalExceptionHandler {

	private static final String REQUEST_POST = "POST";
	private static final String REQUEST_GET = "GET";

	private Log log = LogFactory.getLog(this.getClass());

	@Autowired
	private MessageUtil messageUtil;

	@Autowired
	private ResponseUtil responseUtil;

	/**
	 * @Title: MyErrorHandler
	 * @Description: 自定义异常捕获
	 * @param req
	 * @param e
	 * @throws Exception
	 * @return ResultPojo
	 */
	@ExceptionHandler(value = MyException.class)
	@ResponseBody
	public Map<String, Object> myErrorHandler(HttpServletRequest req, MyException e) {

		// 获取指定组件错误码、错误信息
		String errorCode = e.getMessage();
		String errorMsg = messageUtil.getMessage(e.getMessage());

		// 获取通用错误码信息
		if (StrUtil.isEmpty(errorMsg)) {
			errorMsg = ResponseCodeUtil.getProperty(errorCode);
		}

		// 若错误码未有对应错误信息
		if (StrUtil.isEmpty(errorMsg)) {
			errorCode = ResponseCode.CUSTOM_EXCEPTION_DESC;
			errorMsg = e.getMessage();
		}

		log.warn(errorMsg, e);
		return responseUtil.renderMsg(errorCode, errorMsg, null);
	}

	/**
	 * @Title: systemErrorHandler
	 * @Description: 系统异常捕获
	 * @param req
	 * @param e
	 * @throws Exception
	 * @return ResultPojo
	 */
	@ExceptionHandler(value = Exception.class)
	@ResponseBody
	public Map<String, Object> systemErrorHandler(HttpServletRequest req, Exception e) {
		e.printStackTrace();
		if (e instanceof HttpRequestMethodNotSupportedException) {
			String msg = e.getMessage();

			if (msg.contains(REQUEST_GET)) {
				Map<String, Object> errorMap = responseUtil.renderMsg(ResponseCode.SYSTEM_HTTP_POST,
						ResponseCodeUtil.getProperty(ResponseCode.SYSTEM_HTTP_POST), null);
				log.error(ResponseCodeUtil.getProperty(ResponseCode.SYSTEM_HTTP_POST), e);
				return errorMap;
			}

			if (msg.contains(REQUEST_POST)) {
				Map<String, Object> errorMap = responseUtil.renderMsg(ResponseCode.SYSTEM_HTTP_GET,
						ResponseCodeUtil.getProperty(ResponseCode.SYSTEM_HTTP_GET), null);
				log.error(ResponseCodeUtil.getProperty(ResponseCode.SYSTEM_HTTP_GET), e);
				return errorMap;
			}
		}

		Map<String, Object> errorMap = responseUtil.renderMsg(ResponseCode.SYSTEM_ERROR,
				ResponseCodeUtil.getProperty(ResponseCode.SYSTEM_ERROR), null);
		log.error(ResponseCodeUtil.getProperty(ResponseCode.SYSTEM_ERROR), e);
		return errorMap;
	}

	@ExceptionHandler
	@ResponseBody
	public  Map<String, Object> rightsErrorHandler(AuthorizationException e) {
		return responseUtil.renderMsg(ResponseCode.NO_RIGHTS, "无权访问", null);
	}
}
